package com.dfdchem.imd.kaiwu.util;

import android.net.Uri;

import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;

public class UrlParse {
    private static final String TAG = "UrlParse";

    /**
     * 获得解析后的URI参数
     * @param Uri uri对象
     * @return URL参数map集合
     */
    public static Map<String, String> getUriParams(Uri uri){
        final Map<String, String> query_pairs = new LinkedHashMap<String, String>();

        try {
            String query = uri.getQuery();
            if(query == null){
                return query_pairs;
            }
            //除url之外的参数进行解析
            if(query.length()>0) {
                final String[] pairs = query.split("&");
                for (String pair : pairs) {
                    final int idx = pair.indexOf("=");
                    //如果等号存在且不在字符串两端，取出key、value
                    if (idx > 0 && idx < pair.length() - 1) {
                        final String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
                        final String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
                        query_pairs.put(key, value);
                    }
                }
            }
        }catch (Exception ex){
            Log.e(TAG, ex.getMessage());
        }
        return query_pairs;
    }

    /**
     * 获得解析后的query参数
     * @param Uri uri对象
     * @return URL参数map集合
     */
    public static Map<String, String> getQueryParams(String query){
        final Map<String, String> query_pairs = new LinkedHashMap<String, String>();
        if(query == null){
            return query_pairs;
        }
        try {
            if(query.length()>0) {
                final String[] pairs = query.split("&");
                for (String pair : pairs) {
                    final int idx = pair.indexOf("=");
                    //如果等号存在且不在字符串两端，取出key、value
                    if (idx > 0 && idx < pair.length() - 1) {
                        final String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
                        final String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
                        query_pairs.put(key, value);
                    }
                }
            }
        }catch (Exception ex){
            Log.e(TAG, ex.getMessage());
        }
        return query_pairs;
    }

    /**
     * 获得url的协议+域+路径（即url路径问号左侧的内容）
     * @param url url地址
     * @return url的协议+域+路径
     */
    public static String getUrlHostAndPath(String url){
        if (url.contains("?")) {
            return url.substring(0, url.indexOf("?"));
        }
        return url;
    }

    /**
     * 获得Uri参数值
     * @param uri uri
     * @param paramKey 参数名称
     * @return 参数值
     */
    public static String getUriParam(Uri uri, String paramKey){
        if(uri == null || paramKey==null || paramKey.length() == 0){
            return "";
        }
        String paramValue = uri.getQueryParameter(paramKey);
        if(paramValue == null){
            paramValue = "";
        }
        return paramValue;
    }
}
